
public class Main {
    public static void main(String[] args) {
        System.out.println(new Solution().rob(new int[]{2,3,2}));
        System.out.println(new Solution().rob(new int[]{1,2,3,1}));
        System.out.println(new Solution().rob(new int[]{1,2,3}));
    }
}


class Solution {
    public int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        return Math.max(robRange(nums, 0, nums.length - 2), robRange(nums, 1, nums.length - 1));
    }

    int robRange(int[] nums, int l, int r) {
        int first = nums[l];
        int second = Math.max(nums[l], nums[l + 1]);
        for (int i = 2; l + i <= r; i++) {
            int temp = second;
            second = Math.max(second, first + nums[l + i]);
            first = temp;
        }
        return second;
    }
}
